<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/bootstrap.min.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/jedate.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/fonts/iconfont.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="../../../../resources/layui/css/layui.css"/>
		<style>
			.deteleButton{display: block;}
			.diy .addButton{display: block!important;}
			.diy .deteleButton{display: none!important;}
			.addButton{display: none;}

		</style>
	</head>
	<body>
		<div class="container-fluid">
			<div class="row mheader">
				<div class="col-xs-2 text-left"> <a href="javascript:;" onclick="javascript:history.back(-1);"><span class="iconfont icon-fanhui"></span></a> </div>
				<div class="col-xs-8"><h2>编辑材料申请</h2></div>
				<div class="col-xs-2 text-right" onclick="tj()"><a href="###">修改</span></a></div>
			</div>
			<div class="row">
				<div class="com-form ">
					<form class="form-horizontal">
						<div class="form-group">
					    	<label class="col-xs-3 control-label" for="">工程名称:</label>
					    	<div class="col-xs-9">
					    		<div class="select-box">
						    		<span class="caret"></span>
									<select class="form-control" id="proName">
									  <option>西三环通讯管道施工</option> 
									  <option>花园路通讯管道施工</option>
									</select>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-3 control-label" for="">供货时间:</label>
							<div class="col-xs-9">
								<input type="text" class="layui-input" id="gtime" name="supply_Time" placeholder="请输入供货时间">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-3 control-label" for="">收货地址:</label>
							<div class="col-xs-9">
								<input type="text" class="form-control" id="" placeholder="" value="" name="f_adr">
							</div>
						</div>
					  	<div class="form-group">
					    	<label class="col-xs-3 control-label" for="">申请人:</label>
					    	<div class="col-xs-9">
					    		<input type="text" class="form-control caigouren" id="" placeholder="" value="">
					  		</div>
					    </div>
						<div class="cailias">
							<div class="addMaterial diy">
								<a href="javascript:;" class="mBtn addButton"><i
										class="iconfont icon-jia1"></i></a>
								<a href="javascript:;" class="mBtn deteleButton"><i class="iconfont icon-jian"></i></a>
								<div class="divider"></div>
								<div class="form-group">
									<label class="col-xs-3 control-label" for="">材料名:</label>
									<div class="col-xs-9">
										<div class="select-box">
											<span class="caret"></span>
											<select class="form-control mat_id" onchange="loadBit(this)">

											</select>
										</div>
									</div>
								</div>
								<div class="form-group">
									<label class="col-xs-3 control-label" for="">申请数量:</label>
									<div class="col-xs-9">
										<input type="text" class="form-control" placeholder="" value="0" name="fnum">
									</div>
								</div>
								<div class="form-group">
									<label class="col-xs-3 control-label" for="">计量单位:</label>
									<div class="col-xs-9">
										<input type="text" readonly class="form-control" placeholder="" name="mat_bit"
											   value="">
									</div>
								</div>
							</div>

						</div>
					</form>
				</div>
			</div>
			
		</div>
	</body>
</html>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="../../../resource/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/jquery.jedate.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../resource/js/style.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../resources/js/template-web.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../resources/layui/layui.js"></script>

<script>
    layui.use('laydate', function() {
        var laydate = layui.laydate;

        laydate.render({
            elem: '#gtime'
            ,type: 'datetime'
        });
    })
    var s=true;
    var htmls;
    function tj() {
        var obj = sessionStorage.getItem("obj");
        //传入id来进行渲染
        var d= JSON.parse(obj);
        var cc_UUID = $("#proName option:selected").val();
        var supply_Time = $("input[name='supply_Time']").val();
        var feed_sqr = $("input[name='feed_sqr']").val();
        var f_adr = $("input[name='f_adr']").val();
        var sd = '';
        var shenqingR=$('.caigouren').val();
        var n=0;
        var panduans=true;
        var c=true;
        $('.addMaterial [name="fnum"]').each(function () {
            console.log($(this).attr('mat-type'))
            if($(this).val()==0){
                window.tool.alert('申请材料数不能小于1');
                panduans=false;
                return;
            }
            var m=$(this).attr('mat-type');
            if($(this).attr('matid')==168){
                return;
            }
            //todo:2018-3-27 10:12:55去掉的
/*            if(m==1){
                c=false;
                n+=parseInt($(this).val());
                console.log(s)
                if(n<3&&s){
                    window.tool.alert('上覆材料类总量少于3，需支付运费');
                    panduans=false;
                    console.log(htmls)
                    $('.addMaterial:last').after('<div class="addMaterial yunfei ">'+htmls+'</div>');
                    $('.yunfei').children('.form-group').eq(0).children('.col-xs-9').children('.select-box').children('select').val(168).attr('disabled','disabled');
                    $('.yunfei').children('.form-group').eq(1).children('.col-xs-9').children('input').val(1).attr('disabled','disabled');
                    $('.yunfei').children('.form-group').eq(2).children('.col-xs-9').children('input').val('次').attr('disabled','disabled');
                    s=false;
                }else if(n>=3){
                    $('.yunfei').remove();
                    s=true;
                }
            }*/
        });
        if(c){
            $('.yunfei').remove();
        }



        $(".addMaterial").each(function () {
            //获取材料id
            sd += $(this).find(".mat_id option:selected").val() + ',';
            //数量
            sd += $(this).find("input[name='fnum']").val() + '|';
        });
        sd = sd.substr(0,sd.lastIndexOf("|"));
        if(panduans) {

            ajaxs('api/ctteam/updateMatFeed.do', 'post', {
                    'cc_UUID': cc_UUID,
                    'supply_Time': supply_Time,
                    'feed_sqr': feed_sqr,
                    'f_adr': f_adr,
                    'sd': sd,
                    'feed_sqr': shenqingR,
                    'feed_Code': d.feed_code,
                    'p': getP()
                }, function (data) {
                    window.tool.alert(data.msg);
                location.href="./material-list.html";
                    console.log(data)
                }
            );
        }
    }

    function loadBit(obj){
        //取选中项单位
        var bit = obj.options[obj.options.selectedIndex].dataset.bit;
        var mat=$(obj).find("option:selected").attr("mat-type");
        $(obj).parents('.addMaterial').children().next().next().next().next().children('.col-xs-9').children('input').attr('mat',mat);

        //赋值单位
        $(obj).parents('.addMaterial').find('[name="mat_bit"]').val(bit);
    };
    $(document).ready(function() {
        //获取列表页的内容
        var obj = sessionStorage.getItem("obj");
        //传入id来进行渲染
        var d= JSON.parse(obj);
        //获取合同下拉
        ajaxs('api/public/loadPriDropDown.do','post',{'type':'d','p':getP()},function (data) {
				var html = '';
				$.each(data.data,function (i,item) {
					//合同下拉
					html += '<option value="'+item.cc_UUID+'">'+item.proName+'</option> ';
				});
				$("#proName").html(html);
			});
        //获取材料下拉
        ajaxs('api/public/loadPriDropDown.do','post',{'type':'b','p':getP()},function (data) {
                var html = '<option value="">---请选择---</option>';
                $.each(data.data,function (i,item) {
                    //材料下拉
                    html += '<option value="'+item.mat_id+'" data-bit="'+item.mat_Bit+'" mat-type="'+item.mat_Type+'">'+item.mat_Name+'</option> ';
                });
                $(".addMaterial").each(function () {
                    $(this).find(".select-box .form-control").html(html);
                })
            htmls=$('.addMaterial').html();
            ajaxs('api/material/loadMatFeedPic.do','post',{'type':'b','feed_code':d.feed_code,'p':getP()},function (data) {
                console.log(data);
                var d=data.data;
                var htmls=$('.addMaterial').html();
				$('.cailias').html('');
                //传入id来进行渲染
                $('#proName').val(d[0].cc_uuid);
                $('#gtime').val(d[0].supply_Time);
                $('.caigouren').val(d[0].feed_sqr);
                $('[name="f_adr"]').val(d[0].f_adr);

                for(var i=0;i<d.length;i++){
					$('.cailias').append('<div class="addMaterial">'+htmls+'</div>')
					$('.mat_id').eq(i).val(d[i].mat_id)
                    $('[name="mat_bit"]').eq(i).val(d[i].mat_bit);
                    $('[name="fnum"]').eq(i).val(d[i].feed_Num).attr({'mat-type':d[i].mat_Type,'matid':d[i].mat_id});
                }
                $('.addMaterial').eq(0).addClass('diy');
                $('[name="fnum"]').each(function (k,v) {
                    if($(v).attr('matid')==168){
                        $(v).parents('.addMaterial').addClass('yunfei');
                        s=false;
                        $(v).attr('disabled','disabled');
                        $(v).parents('.yunfei').children('.form-group').eq(0).children('.col-xs-9').children('.select-box').children('select').attr('disabled','disabled');
                        $(v).parents('.yunfei').children('.form-group').eq(2).children('.col-xs-9').children('input').attr('disabled','disabled');
                    }

                })

            });
        });

        $(function(){
            $(document).on('click','.addButton',function () {
                var _htmls=$(this).parent().html();
                $(this).parent().after('<div class="addMaterial">'+_htmls+'</div>');
            });
            $(document).on('click','.deteleButton',function () {
                $(this).parent().remove();
            });


        });

        $(function () {

            //判断数量输入框是否为空
            $('form').on('focus','[name="fnum"]',function () {
                console.log($(this))
                $(this).keyup(function () {
                    console.log($(this).val()<=0)
                    if($(this).val()<=0){
                        $('.text-right').hide();
                        window.tool.alert('数量不能小于0,请修改');
                        return;
                    }else{
                        $('.text-right').show();
                    }
                })
            })


        })
    	
	});
</script>






